#include "QmitkDataPositioning.h"
// #include <qaction.h>

#include <mitkGlobalInteraction.h>
#include <mitkProperties.h>
#include <mitkStringProperty.h>
#include <mitkIDataStorageService.h>
#include <mitkDataNodeFactory.h>
#include <mitkSurface.h>
#include <mitkSurfaceGLMapper2D.h>



#include <mitkNodePredicateDataType.h>

#include <berryIEditorPart.h>
#include <berryIWorkbenchPage.h>
#include <berryPlatform.h>
#include <itkObject.h>
#include <itkCommand.h>

#include "WxPointedSticksMapper2D.h"
#include "WxDoNothingMapper.h"

/*
QmitkDataPositioning::QmitkDataPositioning(QObject *parent, const char *name, QmitkStdMultiWidget *mitkStdMultiWidget, mitk::DataTreeIteratorBase* it)
  : QmitkFunctionality(parent, name, it) , m_MultiWidget(mitkStdMultiWidget), m_Controls(NULL)
{
  SetAvailability(true);
}
*/

QmitkDataPositioning::QmitkDataPositioning()
{
}
QmitkDataPositioning::~QmitkDataPositioning()
{
}

void QmitkDataPositioning::Activated()
{
   m_ItkEventListener = new ItkEventListener(this->GetActiveStdMultiWidget());
   std::vector<mitk::DataNode*> selection = this->GetDataManagerSelection();
   this->OnSelectionChanged( selection );

}

void QmitkDataPositioning::Deactivated()
{
  std::vector<mitk::DataNode*> selection;
  this->OnSelectionChanged( selection );
  m_ItkEventListener->Delete();
}

void QmitkDataPositioning::AddDistanceDataPositioning()
{

    mitk::DataNodeFactory::Pointer nodeReader=mitk::DataNodeFactory::New();
    try
    {
            nodeReader->SetFileName("normalroutemodel2.stl");
            nodeReader->Update();
    }
    catch (...)
    {
            return;
    }

    mitk::DataNode::Pointer node = nodeReader->GetOutput();
    node->GetData()->SetOrigin(this->GetActiveStdMultiWidget()->GetCrossPosition());
    this->GetDataStorage()->Add(node);
    m_CreatedNodes.push_back( node );


    std::vector<mitk::DataNode*> selection;
    selection.push_back( node );
    this->FireNodesSelected( selection );
    this->OnSelectionChanged( selection );
}

void QmitkDataPositioning::CreateQtPartControl( QWidget* parent )
{
  m_Controls = new Ui::QmitkDataPositioningControls;
  m_Controls->setupUi(parent);

  connect( (QObject*)(m_Controls->btnAddRoute), SIGNAL(clicked()),(QObject*) this, SLOT(AddDistanceDataPositioning()) );

}

void QmitkDataPositioning::OnSelectionChanged( std::vector<mitk::DataNode*> nodes )
{
  mitk::DataNode* selectedNode = 0;
  if(nodes.size() > 0)
    selectedNode = nodes.front();

  if(m_OldData.IsNotNull())
  {
      m_OldData->GetData()->RemoveObserver(m_ObserverTag);
      m_OldData->SetMapper(mitk::BaseRenderer::Standard2D,mitk::SurfaceGLMapper2D::New());
  }
  if(m_PointedSticksInteractor.IsNotNull())
      mitk::GlobalInteraction::GetInstance()->RemoveListener(m_PointedSticksInteractor);
  mitk::Surface* surface = 0;
  if(selectedNode)
    surface = dynamic_cast<mitk::Surface*> ( selectedNode->GetData() );

  bool modelCreatedByThis = false;

  this->GetActiveStdMultiWidget()->EnableNavigationControllerEventListening();
  mitk::GlobalInteraction::GetInstance()->AddListener( this->GetActiveStdMultiWidget()->GetMoveAndZoomInteractor());
  if(surface)
  {
    // see if this pointset was created by us
    std::vector<mitk::DataNode*>::iterator it = std::find( m_CreatedNodes.begin()
      , m_CreatedNodes.end(), selectedNode);
    if(it != m_CreatedNodes.end())
     modelCreatedByThis = true;
  }

  if(modelCreatedByThis && this->IsActivated())
  {
      m_ObserverTag=surface->AddObserver(itk::ModifiedEvent(), m_ItkEventListener);
      m_OldData=selectedNode;
      selectedNode->SetMapper(mitk::BaseRenderer::Standard2D,mitk::WxPointedSticksMapper2D::New());
      surface->Modified();

      this->GetActiveStdMultiWidget()->DisableNavigationControllerEventListening();
      mitk::GlobalInteraction::GetInstance()->RemoveListener( this->GetActiveStdMultiWidget()->GetMoveAndZoomInteractor());
      mitk::WxPointedSticksInteractor::Pointer newPointedSticksInteractor
              = mitk::WxPointedSticksInteractor::New("PointedSticksInteractor", selectedNode);
      mitk::GlobalInteraction::GetInstance()->AddListener(newPointedSticksInteractor);
      this->m_PointedSticksInteractor=newPointedSticksInteractor;

  }
  else
  {
    // revert text

  }
}

bool QmitkDataPositioning::IsExclusiveFunctionality() const
{
  return true;
}

void QmitkDataPositioning::NodeRemoved( const mitk::DataNode* node )
{
  // remove a node if it is destroyed from our created array
  std::vector<mitk::DataNode*>::iterator it = std::find( m_CreatedNodes.begin()
    , m_CreatedNodes.end(), node);
  if(it != m_CreatedNodes.end())
    m_CreatedNodes.erase(it);
}


